package com.gnerv.battle.boot.security.autoconfigure;

import com.gnerv.battle.boot.security.config.filter.BattleCustomVerificationCodeFilter;
import com.gnerv.battle.boot.security.service.AbstractUrlPermissionService;
import com.gnerv.battle.boot.security.service.AbstractVerificationCodeService;
import com.gnerv.battle.boot.security.service.impl.UrlPermissionServiceImpl;
import com.gnerv.battle.boot.security.service.impl.VerificationCodeServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Configuration
@ConditionalOnWebApplication
@ComponentScan(value = "com.gnerv.battle.boot.security")
@EnableConfigurationProperties(BattleSecurityConfigProperties.class)
public class BattleSecurityAutoConfiguration {

    @Resource
    private BattleSecurityConfigProperties battleSecurityConfigProperties;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @ConditionalOnMissingBean
    public BattleCustomVerificationCodeFilter battleCustomVerificationCodeFilter() {
        return new BattleCustomVerificationCodeFilter(battleSecurityConfigProperties.getLoginUrl());
    }

    @Bean
    @ConditionalOnMissingBean
    public AbstractVerificationCodeService verificationCodeService() {
        return new VerificationCodeServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean
    public AbstractUrlPermissionService urlPermissionService() {
        return new UrlPermissionServiceImpl();
    }


}
